package com.zpf.controller;

import com.zpf.pojo.User;
import com.zpf.service.AdminService;
import com.zpf.service.impl.AdminServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/admin")
public class AdminController extends BaseServlet{

//管理院登录
    public String login(HttpServletRequest request, HttpServletResponse response){

        //1.获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2.判断用户名密码不为空，如果为空则跳转登录界面
        if(username == null || password == null){
            return "redirect:"+request.getContextPath()+"admin/login.jsp";
        }


        //3.调用service校验用户名密码
        AdminService adminService = new AdminServiceImpl();
        User user = adminService.login(username,password);

        //4.将登录成功后的用户对象放入tomcat的session会话中一直携带
        //5.根据结果判断进行重定向操作
        if(user != null){
            request.getSession().setAttribute("admin",user);

            //6.重定向,跳转到管理员首页
            return "redirect:"+request.getContextPath()+"/admin/admin.jsp";

        }else {
            //7.用户名密码不正常重新登录
            return "redirect:"+request.getContextPath()+"/admin/login.jsp";
        }

    }
}

